package com.iiapk.gridview;

import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.iiapk.gridview.animation.AnimationUtils;
import com.iiapk.gridview.database.DataSourceTest;
import com.iiapk.gridview.database.GridDataBase;
import com.iiapk.gridview.database.GridItem;
import com.iiapk.gridview.view.DragGridView;
import com.iiapk.gridview.view.ScrollLayout;

public class GridViewTestActivity extends Activity {
	
	private ScrollLayout scrollLayout;
	private GridDataBase gridDataBase;
	private List<GridItem> list = new ArrayList<GridItem>();
	private TextView tvpage;
	private ImageView runImage;
	private Animation rightAnimation;
	private Animation leftAnimation;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Constant.init(this);
				
        scrollLayout = (ScrollLayout)findViewById(R.id.scrollLayout);
        tvpage = (TextView) findViewById(R.id.tv_page);
		tvpage.setText("1");
		runImage = (ImageView) findViewById(R.id.run_image);
		setImageBgAndRun();
		
        gridDataBase = new GridDataBase(this);
        initDataSource();
        
        addGridView();
        addGridView();
        addGridView();
        ScrollLayout.countPages=3;
        
        scrollLayout.setPageListener(new ScrollLayout.PageListener() {
			@Override
			public void page(final int page) {
				Animation animation = AnimationUtils.getScaleAnimation(1.0f, 0.0f, 1.0f, 1.0f, 300);
				animation.setAnimationListener(new Animation.AnimationListener() {
					@Override
					public void onAnimationStart(Animation animation) {
					}
					@Override
					public void onAnimationRepeat(Animation animation) {
					}
					@Override
					public void onAnimationEnd(Animation animation) {
						tvpage.setText((page + 1) + "");
						tvpage.startAnimation(AnimationUtils.getScaleAnimation(0.0f, 1.0f, 1.0f, 1.0f, 300));
					}
				});
				tvpage.startAnimation(animation);
			}
		});
    }
    
    public void initDataSource(){
    	gridDataBase.open();
    	if(!gridDataBase.hasItems()){
    		gridDataBase.insertItems(new DataSourceTest().getList());
    	}
    	list = gridDataBase.getItems();
    }
    
    public void addGridView() {
    	LinearLayout.LayoutParams param = new LinearLayout.LayoutParams(
				android.view.ViewGroup.LayoutParams.FILL_PARENT,
				android.view.ViewGroup.LayoutParams.FILL_PARENT);
		param.rightMargin = 100;
		param.leftMargin = 20;
		LinearLayout linear = new LinearLayout(this);
		linear.addView(new DragGridView(this,list), param);
		scrollLayout.addView(linear);
    }
    
    public void setImageBgAndRun() {
    	Bitmap bitmap = BitmapFactory.decodeStream(getResources().openRawResource(R.drawable.loading));
		int bitmap_width = bitmap.getWidth();
		rightAnimation = new TranslateAnimation(Animation.ABSOLUTE, 0f,Animation.ABSOLUTE, 
				-bitmap_width + Constant.screenWidth,Animation.ABSOLUTE, 0f, Animation.ABSOLUTE, 0f);
		leftAnimation = new TranslateAnimation(Animation.ABSOLUTE, -bitmap_width+ Constant.screenWidth, Animation.ABSOLUTE,
				0f, Animation.ABSOLUTE, 0f,Animation.ABSOLUTE, 0f);
		
		rightAnimation.setDuration(25000);
		leftAnimation.setDuration(25000);
		rightAnimation.setFillAfter(true);
		leftAnimation.setFillAfter(true);
		
		rightAnimation.setAnimationListener(new Animation.AnimationListener() {
			@Override
			public void onAnimationStart(Animation animation) {
			}
			@Override
			public void onAnimationRepeat(Animation animation) {
			}
			@Override
			public void onAnimationEnd(Animation animation) {
				runImage.startAnimation(leftAnimation);
			}
		});
		leftAnimation.setAnimationListener(new Animation.AnimationListener() {
			@Override
			public void onAnimationStart(Animation animation) {
			}
			@Override
			public void onAnimationRepeat(Animation animation) {
			}
			@Override
			public void onAnimationEnd(Animation animation) {
				runImage.startAnimation(rightAnimation);
			}
		});
		runImage.startAnimation(rightAnimation);
    }
    
}